﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Lilium.Configuration;
using System.Reflection;
using Lilium.Web.Mvc.MicrosoftAjax;
using System.Web.UI;

namespace Lilium.Web.Mvc.Configuration
{
	public class ScriptResourceLibraryElementCollection: ConfigurationElementCollection<ScriptResourceLibraryElement>
	{
		public ScriptResourceLibraryElementCollection()
		{
			AddMicrosoftAjaxEmbeededConfiguration();
			AddAjaxControlToolkitEmbeededConfiguration();
		}

		protected override object GetElementKey(ConfigurationElement element)
		{
			if (element == null)
				throw new ArgumentNullException("element");

			return ((ScriptResourceLibraryElement)element).Name;
		}

		private void AddMicrosoftAjaxEmbeededConfiguration()
		{
			var microsoftAjaxLibraryElement = new EmbeededResourceLibraryElement
			{
				ContentType = "text/javascript",
				Name = MicrosoftAjax.Constants.AjaxLibraryName,
				OutputCache = new OutputCacheConfigurationElement
				{
					Duration = 86400,
					Location = OutputCacheLocation.Client,
				},
				ResourceAssembly = typeof(MicrosoftAjax.Constants).Assembly,
				ResourceNamespace = typeof(MicrosoftAjax.Constants).Namespace,
			};

			var cultureInfoElement = new MicrosoftAjaxCultureInfoResourceElement
			{
				ContentType = "text/javascript",
				Name = MicrosoftAjax.Constants.CultureInfoResourceName,
			};
			microsoftAjaxLibraryElement.Resources.Add(cultureInfoElement);

			Add(microsoftAjaxLibraryElement);
		}

		private void AddAjaxControlToolkitEmbeededConfiguration()
		{
			var ajaxControlToolkitLibraryElement = new EmbeededResourceLibraryElement
			{
				ContentType = "text/javascript",
				Name = AjaxControlToolkit.Constants.ToolkitLibraryName,
				OutputCache = new OutputCacheConfigurationElement
				{
					Duration = 86400,
					Location = OutputCacheLocation.Client,
				},
				ResourceAssembly = typeof(AjaxControlToolkit.Constants).Assembly,
				ResourceNamespace = typeof(AjaxControlToolkit.Constants).Namespace,
			};

			// base scripts (client resource)

			var baseScriptsElement = new EmbeededToolkitScriptElement
			{
				Name = "ExtenderBase.BaseScripts.js"
			};
			ajaxControlToolkitLibraryElement.Resources.Add(baseScriptsElement);

			var baseScriptsClientResourcesElement = new ToolkitScriptResourceElement
			{
				Name = "ScriptResources",
				ResourceTypeName = AjaxControlToolkit.Constants.ToolkitLibraryName + ".Resources",
			};
			baseScriptsElement.ClientResources.Add(baseScriptsClientResourcesElement);


			// calendar (resources)
			var calendarStyleElement = new EmbeededResourceElement
			{
				ContentType = "text/css",
				Name = "Calendar.Calendar.css",
			};
			ajaxControlToolkitLibraryElement.Resources.Add(calendarStyleElement);

			var arrowLeftElement = new EmbeededResourceElement
			{
				ContentType = "image/gif",
				Name = "Calendar.arrow-left.gif",
			};
			ajaxControlToolkitLibraryElement.Resources.Add(arrowLeftElement);

			var arrowRightElement = new EmbeededResourceElement
			{
				ContentType = "image/gif",
				Name = "Calendar.arrow-right.gif",
			};
			ajaxControlToolkitLibraryElement.Resources.Add(arrowRightElement);

			// color picker
			var colorPickerStyleElement = new EmbeededResourceElement
			{
				ContentType = "text/css",
				Name = "ColorPicker.ColorPicker.css"
			};
			ajaxControlToolkitLibraryElement.Resources.Add(colorPickerStyleElement);

			Add(ajaxControlToolkitLibraryElement);
		}
	}
}